﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


    public partial class ViewSupplierForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Search then select the row which matched with the input name
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void searchButt_Click(object sender, EventArgs e)
        {
            try
            {
                listSupplier.SelectedIndex = -1;
                foreach (var item in listSupplier.Items)
                {
                    Label i = (Label)item.FindControl("NameLabel");
                    if (searchBox.Text.Equals(i.Text.Trim()))
                    {
                        listSupplier.SelectedIndex = item.DisplayIndex;
                    }
                }
            }
            catch (Exception ex)
            {

                searchTxt.Text = "Error: " + ex.Message;
            }

        }

        /// <summary>
        /// Actions occurred when user click on the link buttons in the list
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void listSupplier_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                //Action if user click on Insert
                if (e.CommandName.Equals("Insert"))
                {
                    TextBox name = (TextBox)e.Item.FindControl("NameTextBox");
                    TextBox address = (TextBox)e.Item.FindControl("AddressTextBox");
                    TextBox phone = (TextBox)e.Item.FindControl("PhoneTextBox");
                    TextBox fax = (TextBox)e.Item.FindControl("FaxTextBox");
                    string insertComm =
                        "INSERT INTO [Supplier] ([Name], [Address], [Phone], [Fax]) VALUES('"
                        + name.Text + "', '" + address.Text + "', '" + phone.Text
                        + "', '" + fax.Text + "');";
                    listSupplierData.InsertCommand = insertComm;
                }
                else if (e.CommandName.Equals("Update"))    //Action if user click on Update
                {
                    Label id = (Label)e.Item.FindControl("SupplierIDLabel1");
                    TextBox name = (TextBox)e.Item.FindControl("NameUp");
                    TextBox address = (TextBox)e.Item.FindControl("AddressUp");
                    TextBox phone = (TextBox)e.Item.FindControl("PhoneUp");
                    TextBox fax = (TextBox)e.Item.FindControl("FaxUp");
                    string updateComm =
                        "UPDATE [Supplier] SET [Name]='" + name.Text
                            + "', [Address]='" + address.Text
                            + "', [Phone]='" + phone.Text
                            + "', [Fax]='" + fax.Text
                            + "' WHERE [SupplierID]=" + int.Parse(id.Text) + ";";
                    listSupplierData.UpdateCommand = updateComm;
                }
                else if (e.CommandName == "Delete") //Action if user click on Delete
                {
                    Label id = (Label)e.Item.FindControl("SupplierIDLabel1");
                    string deleteComm =
                        "DELETE FROM [Supplier] WHERE [SupplierID]=" + int.Parse(id.Text);
                    listSupplierData.DeleteCommand = deleteComm;
                }
            }
            catch (Exception ex)
            {

                searchTxt.Text = "Error: " + ex.Message;
            }
            
        }

}

